package fi.iki.juri.units.generator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;


/**
 * Generates the class fi.iki.juri.units.generated.GeneratedUnits.
 *
 * @author juri
 */
public class Generator {
	public String generate(final Iterable<String> specFiles, final ArrayList<String> transFiles, final CodeGenerator codegen) throws FileNotFoundException {
		final SpecificationParser specParser = new SpecificationParser();
		final TranslationParser transParser = new TranslationParser();
		final Collection<ParsedUnit> units = new ArrayList<ParsedUnit>();
		for (final String file : specFiles) {
			final InputStream stream = new FileInputStream(file);
			units.addAll(specParser.parse(stream));
		}
		final Collection<ParsedTranslations> translations = new ArrayList<ParsedTranslations>();
		for (final String file : transFiles) {
			final InputStream stream = new FileInputStream(file);
			translations.add(transParser.parse(stream));
		}
		return codegen.generate(units, translations);
	}

	
	public static void main(final String[] args) {
		if (args.length != 5) {
			System.err.println("The JUGC generator takes four arguments:");
			System.err.println("<specification files> <translation files> <target directory> <class/module name> <language>");
			System.err.println();
			System.err.println("The first two arguments are lists of files, separated by the ");
			System.err.println("CLASSPATH separator.");
			System.err.println("Specification files are the files containing unit specifications.");
			System.err.println("Translation files are the files containing translated unit names.");
			System.err.println();
			System.err.println("The class/module name is the name of the class or module to generate.");
			System.err.println("For Java, it should be a fully qualified class name, for Python, just ");
			System.err.println("a plain module name.");
			System.err.println();
			System.err.println("Target directory specifies the directory where to output the generated");
			System.err.println("files.");
			System.err.println();
			System.err.println("Language is the language for which to generate code. Currently");
			System.err.println("supported options are java and python.");
			System.exit(1);
		}
		final Generator g = new Generator();
		final ArrayList<String> specFiles = new ArrayList<String>();
		for (final String file : args[0].split(System.getProperty("path.separator"))) {
			specFiles.add(file);
		}
		final ArrayList<String> transFiles = new ArrayList<String>();
		for (final String file : args[1].split(System.getProperty("path.separator"))) {
			transFiles.add(file);
		}
		CodeGenerator cgen = null;
		final String module = args[3];
		if (args[4].equals("java"))
			cgen = new JavaGenerator(module);
		else if (args[4].equals("python"))
			cgen = new PythonGenerator(module);
		try {
			final String klass = g.generate(specFiles, transFiles, cgen);
			final Writer writer = new OutputStreamWriter(new FileOutputStream(new File(args[2], cgen.outputName())), Charset.forName("UTF-8"));
			writer.write(klass);
			writer.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
